package fr.cnam.ratpProject.client.activities.searchStation;

import java.util.ArrayList;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;

import fr.cnam.ratpProject.client.services.ReseauRatpService;
import fr.cnam.ratpProject.client.services.ReseauRatpServiceAsync;
import fr.cnam.ratpProject.client.utils.GlueProvider;
import fr.cnam.ratpProject.client.utils.RatpActivity;
import fr.cnam.ratpProject.client.utils.RatpCommand;
import fr.cnam.ratpProject.sharedDatas.Station;

public class SearchStation extends RatpActivity{

	
	public interface SearchStationView extends IsWidget
	{
		void setAddCommand(RatpCommand<String> command);

		void setSuggestStations(ArrayList<Station> result);

		void buildAdClass();
	}
	
	
	private SearchStationView view;
	
	private ArrayList<Station> lstAllStations ;

	
	private ReseauRatpServiceAsync ratpService = GWT.create(ReseauRatpService.class);

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		SearchStationPlace place = ((SearchStationPlace)this.here);
		if(place.isClassement())
		{
			view = GlueProvider.INSTANCE.getClientFactory().getSearchView();
			attachView(panel, view);
			view.buildAdClass();
		}
		else
		{
		view = GlueProvider.INSTANCE.getClientFactory().getSearchView();
		attachView(panel, view);
		
//		ratpService.getAllStations(new AsyncCallback<ArrayList<Station>>() {
//			
//
//			@Override
//			public void onSuccess(ArrayList<Station> result) {
//				lstAllStations = result;
//				view.setSuggestStations(result);
//			}
//			
//			@Override
//			public void onFailure(Throwable caught) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		
		view.setAddCommand(new RatpCommand<String>() {		

			@Override
			public void execute(String stationName) {
				if(stationName !=null && lstAllStations !=null)
				{
					Station stationToSend = null;
					
					for(Station station:lstAllStations)
					{
						if(station.getName().equals(stationName))
						{
							stationToSend = station;
							break;
						}
					}
//					if(stationToSend!=null)
//					{
//						ShowStationPlace placeToGo = new ShowStationPlace("ShowStation");
//						placeToGo.setStation(stationToSend);
//						goTo(placeToGo);
//					}
					
				}
				
				goTo(new ShowStationPlace("ShowStation"));
//				new AlertDialog("Erreur billy", "Elle est pas bonne ta station billy").show();
			}
		});
		}
	}
}
